package com.jams.goodox.activity.mine.address;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.home.ChooseAddressFirstActivity;
import com.jams.goodox.constant.UrlConstants;
import com.jams.goodox.entity.User;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.EditTextUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Gu on 2017/7/11.
 * Description 我的服务地址- 新增
 */
public class MineAddressAddActivity extends BaseActivity {

    @Bind(R.id.input_box)
    EditText inputBox;
    @Bind(R.id.input_box2)
    EditText inputBox2;
    @Bind(R.id.input_box3)
    EditText inputBox3;
    @Bind(R.id.mine_address_submit)
    TextView mineAddressSubmit;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.mask)
    ImageView mask_small;
    @Bind(R.id.parent_layout)
    RelativeLayout parentLayout;
    @Bind(R.id.mask_big)
    RelativeLayout mask; // 全屏的遮罩
    @Bind(R.id.address_default)
    TextView address_default;

    private String city;
    private boolean isChooseDefault;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_address_edit;
    }

    @Override
    protected void init() {
        StyleUtil.init(this, "新增收货地址");
        mineAddressSubmit.setText("确认提交");
        //从缓存里面获取User
        User user = DataCacheManager.getInstance().getUser();
        inputBox.setText(user.getName());
        inputBox2.setText(user.getMobileNum());
        UrlConstants.addressIn="MineAddressAddActivity";
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        city=intent.getStringExtra("name");
        address.setText(city);
    }

    @OnClick({R.id.mine_address_submit,R.id.rl3,R.id.address_default})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mine_address_submit:
                // 提交
                String addressDetail = EditTextUtils.getTextNoSpace(inputBox3);
                String name = EditTextUtils.getTextNoSpace(inputBox);
                String phone = EditTextUtils.getTextNoSpace(inputBox2);

                if (name.length() < 2 || name.length() > 10 || !Utils.justTxt(name)) {
                    ToastUtil.showShort(mContext, "请输入2-8位收货人姓名");
                    return;
                }

                if (!Utils.isPhoneNum(phone)) {
                    ToastUtil.showShort(mContext, "请输入11位手机号");
                    return;
                }

                if (addressDetail.length() < 5) {
                    ToastUtil.showShort(mContext, "请输入5-150字详细地址");
                    return;
                }
                mask.setVisibility(View.VISIBLE);
                Map<String,String> params=new HashMap<>();
                params.put("token",SePref.getToken());
                params.put("id","");
                params.put("name",name);
                params.put("mobile",phone);
                params.put("city",String.valueOf(city));
                params.put("address",addressDetail);
                params.put("use",isChooseDefault?"1":"0");
                closeKeyboard();
                enqueue(createCallService(HomeService.class).addressEdit(params), new RetrofitCallbackListener<BaseEntity>() {
                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                        mask.setVisibility(View.GONE);
                        finish();
                    }

                    @Override
                    public void onError(String msg) {
                        mask.setVisibility(View.GONE);
                    }
                });
                break;
            case R.id.rl3:
                qStartActivity(ChooseAddressFirstActivity.class);
                break;
            case R.id.address_default:
                isChooseDefault=!isChooseDefault;
                address_default.setCompoundDrawablesWithIntrinsicBounds(0,0,isChooseDefault?R.mipmap.check_active:R.mipmap.uncheck_active,0);
                break;
        }
    }

}
